package edu.uow.devdocs
/**
 * The Project entity.
 *
 * @author    
 *
 *
 */
class Project {
	static auditable = true
	// Relation
	System system
	String name
	Date lastUpdated
	Date startDate
	Date endDate
	ProjectStatus status
	Integer estimatedHours
	String description
	Boolean availableToAllFlag
	ProjectType projectType
	String costCentre
	Boolean flagBillable
	java.math.BigDecimal estimatedPercentComplete

	// Relation
	Person businessOwner
	// Relation
	Person developmentManager
	
	static searchable = [only: ['id','description','projectType','name']]
	
	static belongsTo = Person
	
	static transients = ['totalHours']
	
	static hasMany = [persons: Person, commentHistory: CommentHistory]
	
	
    static mapping = {
         table 'SUB_SYSTEM'
         // version is set to false, because this isn't available by default for legacy databases
         version false
         id column:'SUB_SYSTEM_ID'
         businessOwner column:'BUSINESS_OWNER_ID'
         developmentManager column:'DEVELOPMENT_MANAGER_ID'
		 system column: 'SYSTEM_ID'
		 id generator: 'sequence', params:[sequence:'SUB_SYSTEM_SEQ']
		 persons joinTable: [name:'SUB_SYSTEM_PERSONS', key:'PROJECT_ID', column: 'PERSON_ID']
		 sort: name:'asc' 
		 //cache usage: 'read-write'
    }

    static constraints = {
        name(size: 0..100)
        lastUpdated(nullable: true)
        startDate(nullable: true)
        endDate(nullable: true)
        status(nullable: true)
        estimatedHours(nullable: true)
        description(size: 0..10000)
        availableToAllFlag(nullable: true)
        projectType(nullable: true)
        costCentre(size: 0..100)
        estimatedPercentComplete(nullable: true, size: 0..20)
		businessOwner(nullable: true)
	}
	
	static namedQueries = {
		allProjectsForPerson { userId->
			or {
				eq('availableToAllFlag',true)
				persons {
					eq('id',new Long(userId))
				}
			}
			order('name','asc')
		}
		projectsForPerson  { userId->
			persons {
				eq('id',new Long(userId))
			}
			order('name','asc')
		}
	}
	
    String toString() {
        return "${system?.name}/${name}"
    }

	
}
